<?php
namespace app\index\controller;

use think\Request;
use think\Controller;
use think\Session;

//微信xiaoxi
class Wechatmsg extends Controller{
    
    //发送消息
	public function send(){

		$raw = file_get_contents('php://input', 'r');
        $data = json_decode($raw,true);
        if(empty($data['appid']) || empty($data['appsecret'])){
            ajax('appid needed.');
        }
        
        $messageData = [
            'appid' => $data['appid'],
            'touser' => $data['touser'],
            'template_id' => $data['template_id'],
            'data' => $data['data'],
        ];
        
        if(isset($data['url'])){
            $messageData['url'] = $data['url'];
        }
        
        __log($messageData,'wechatmsg');
        
        $hp = new \WechatMessageHelper($data['appid'],$data['appsecret']);
        $response = $hp->sendMsg($messageData);
        
        __log('[back]'.$response,'wechatmsg');
        
        $result = json_decode($response,true);
        $success = false;
        if(
            isset($result['errcode']) && 
            isset($result['errmsg'])
        ){
            $success = $result['errmsg'] == 'ok';
        }
        
        ajax($success ? 'SUCCESS' : 'FAILED',$success);
		
	}
	
}
